import { Options, Vue } from 'vue-class-component'
import navigator from '@/components/common/navigator.vue'
import { ElMessage } from 'element-plus'
import store from '@/store'
import router from '@/router'

interface ISetParams {
  avatar: string
  nickname: string
  email: string
  mobile: string
  realName: string
  gender: string
  birthday: Date
  userDescription: string
}

@Options({
  components: {
    navigator
  }
})

export default class set extends Vue {
  userId: '0'
  setParams: ISetParams = {
    avatar: '',
    nickname: '',
    email: '',
    mobile: '',
    realName: '',
    gender: 'boy',
    birthday: new Date(),
    userDescription: ''
  }

  mounted () {
    if (store.state.u !== null) {
      this.userId = store.state.u.userId
      store.dispatch('getBasicUserInfo', this.userId)
        .then(response => {
          this.setParams = response.data.data
          console.log(this.setParams)
        }).catch(() => {
          console.log('connect failed')
        })
    }
  }

  save (): void {
    store.dispatch('updateBasicInfo', this.setParams)
      .then(response => {
        console.log('connect success')
        const code = response.data.code
        if (code === 200) {
          ElMessage.success({
            message: '修改成功',
            type: 'success'
          })
          router.push({
            path: '/basicInformation',
            query: { data: response.data.data }
          })
        }
      }).catch(() => {
        console.log('connect failed')
      })
  }
}
